package offer;

import java.util.HashMap;

public class offerII_0010 {
    public int subarraySum(int[] nums, int k) {
        int len=nums.length;
        int[] pre=new int[len+1];
        pre[0]=0;
        for(int i=1;i<len+1;++i){
            pre[i]=pre[i-1]+nums[i-1];
        }
        HashMap<Integer, Integer> map=new HashMap<>();
        map.put(0,1);
        int num=0;
        for(int i=1;i<len+1;++i){
            if(map.containsKey(pre[i]-k)) num+=map.get(pre[i]-k);
            if(map.containsKey(pre[i])) map.put(pre[i],map.get(pre[i])+1);
            else map.put(pre[i],1);
        }
        return num;
    }
}
